/*************************************************************************
> File Name: SqList->cpp
> Author: hzy
> Created Time: 2023-08-07 19:02:42
> Description:
************************************************************************/

#include "sqlist.h"

SqList::SqList(int mylength)
{
    if (length <= 0)
    {
        length = 0;
        data = NULL;
    }
    else
    {
        length = mylength;
        data = (int *)calloc(length, sizeof(int));
    }
    last = -1;
}

SqList::~SqList()
{
    if (data != NULL)
    {
        free(data);
        data = NULL;
    }
}

int SqList::InserSqlist(int local, int mydata)
{
    if (last == length - 1)
    {
        cout << "表已满" << endl;
        return -1;
    }

    if (local < 0 || local > last + 1)
    {
        cout << "插入位置不符合规定" << endl;
        return -1;
    }

    for (int i = last; i >= local; i--)
    {
        data[i + 1] = data[i];
    }

    data[local] = mydata;
    last++;

    return 0;
}

void SqList::show()
{
    for (int i = 0; i < last; i++)
    {
        cout << data[i] << "  ";
    }
    cout << endl;
}

#if 0
void SqList::DeleteSqlist(SqList *list, int local)
{
    if (isEmptySqlist(list))
    {
        cout << "表为空" << endl;
        return;
    }

    for (int i = local; i < list->length - 1; i++)
    {
        list->data[i] = list->data[i + 1];
    }

    list->length--;
}

void SqList::SelectSqlist(SqList *list, int local, int *mydata)
{
    if (isEmptySqlist(list))
    {
        cout << "表为空" << endl;
        return;
    }
    if (local < 0 || local > list->length)
    {
        cout << "查询位置不符合规定" << endl;
        return;
    }

    *mydata = list->data[local - 1];
}
#endif
